﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace BanHangCshap.Class
{
    class DonViQuyDoi:Database
    {
        #region ***** Fields & Properties *****
        private int _maquydoi;
        public int maquydoi
        {
            get
            {
                return _maquydoi;
            }
            set
            {
                _maquydoi = value;
            }
        }
        private string _mahang;
        public string mahang
        {
            get
            {
                return _mahang;
            }
            set
            {
                _mahang = value;
            }
        }
        private string _dvtgoc;
        public string dvtgoc
        {
            get
            {
                return _dvtgoc;
            }
            set
            {
                _dvtgoc = value;
            }
        }
        private string _dvtquydoi;
        public string dvtquydoi
        {
            get
            {
                return _dvtquydoi;
            }
            set
            {
                _dvtquydoi = value;
            }
        }
        private int _slquydoi;
        public int slquydoi
        {
            get
            {
                return _slquydoi;
            }
            set
            {
                _slquydoi = value;
            }
        }
        #endregion

        #region ***** Init Methods *****
        public DonViQuyDoi()
        {
        }
        public DonViQuyDoi(int maquydoi)
        {
            this.maquydoi = maquydoi;
        }
        public DonViQuyDoi(int maquydoi, string mahang, string dvtgoc, string dvtquydoi, int slquydoi)
        {
            this.maquydoi = maquydoi;
            this.mahang = mahang;
            this.dvtgoc = dvtgoc;
            this.dvtquydoi = dvtquydoi;
            this.slquydoi = slquydoi;
        }
        #endregion
        public Boolean isExistDonViQuyDoi(string mahang,string dtvquydoi)
        {
            string sql = "select * from DonViQuyDoi where mahang = '" + mahang + "' and dvtquydoi = N'" + dvtquydoi + "'";
            return isExist(sql);
        }
        public DataTable layDSDonViQuyDoi()
        {
            string sql = "select a.mahang,maquydoi,tenhang, dvtgoc,dvtquydoi,slquydoi from HangHoa a left join DonViQuyDoi b on a.mahang = b.mahang order by a.mahang";
            return getDataTable(sql);
        }
        public Boolean themDonViQuyDoi()
        {
            string sql = "insert into DonViQuyDoi(mahang, dvtgoc,dvtquydoi,slquydoi) values ('" + mahang + "',N'" + dvtgoc + "',N'" + dvtquydoi + "'," + slquydoi + ")";
            return executeQuery(sql);
        }
        public Boolean suaDonViQuyDoi()
        {
            string sql = "update DonViQuyDoi set dvtquydoi=N'" + dvtquydoi + "',slquydoi=" + slquydoi + ",dvtgoc = N'" + dvtgoc + "' where maquydoi = " + maquydoi + "";
            return executeQuery(sql);
        }
        public Boolean xoaDonViQuyDoi(int maquydoi)
        {
            string sql = "delete DonViQuyDoi where maquydoi = " + maquydoi + "";
            return executeQuery(sql);
        }
        public DataTable layDonViTinhCuaHH(string mahang)
        {
            string sql = "select dvtquydoi from DonViQuyDoi where mahang = '" + mahang + "'";
            return getDataTable(sql);
        }
        public DataTable layThongTinDonViQuyDoi(int maquydoi)
        {
            string sql = "select * from DonViQuyDoi where maquydoi = " + maquydoi + "";
            return getDataTable(sql);
        }
        public DataTable layThongTinQuyDoiNhap(string mahang, string dvt)
        {
            
            
            string sql = "select slquydoi,gianhap * slquydoi as dgquydoi from DonViQuyDoi a, HangHoa b where a.mahang = b.mahang and a.mahang = '" + mahang + "' and dvtquydoi = N'" + dvt + "'";
            return getDataTable(sql);
          
        }
        public DataTable layThongTinQuyDoiXuat(string mahang, string dvt,string loainhapxuat)
        {
            string gia = "";
            if (loainhapxuat == "PN")
                gia = "gianhap";
            else gia = "giaxuatle";
            string sql = "select slquydoi," + gia + " * slquydoi as dgquydoi from DonViQuyDoi a, HangHoa b where a.mahang = b.mahang and a.mahang = '" + mahang + "' and dvtquydoi = N'" + dvt + "'";
            return getDataTable(sql);

        }
       
    }
}
